package net.baiken.yuki.svn;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JOptionPane;

public class SVNServer {

	private static final String DEFAULT_YUKI_RUNPATH = "J:/Documents/Projects/SVN Portable/Yuki SVN/testdata";

	private static final String YUKI_RUNPATH = "yuki.runpath";

	private static final String DEFAULT_SVN_REPOSITORY = "repository";

	private static final String DEFAULT_SVN_HOME = "bin";

	private static final String SVN_REPOSITORY_KEY = "yuki.svn.repository";

	private static final String SVN_HOME_KEY = "yuki.svn.home";

	private Process SVNProcess;

	private static SVNServer instance;

	private String svnStartCommand;

	public static SVNServer getInstance() {
		if (instance == null) {
			instance = new SVNServer();
		}
		return instance;
	}

	private SVNServer() {
		{// getting the folder paths
			if (!System.getProperties().containsKey(SVN_HOME_KEY)) {
				System.setProperty(SVN_HOME_KEY, DEFAULT_SVN_HOME);
			}
			if (!System.getProperties().containsKey(SVN_REPOSITORY_KEY)) {
				System.setProperty(SVN_REPOSITORY_KEY, DEFAULT_SVN_REPOSITORY);
			}
			if (!System.getProperties().containsKey(YUKI_RUNPATH)) {
				System.setProperty(YUKI_RUNPATH, DEFAULT_YUKI_RUNPATH);
			}
		}

		{// loading env variables
			svnStartCommand = " \"%yuki.runpath%%file.separator%%yuki.svn.home%/bin/svnserve.exe\" -d --root \"%yuki.runpath%%file.separator%%yuki.svn.repository%\" ";
			Map<String, String> env = (Map<String, String>) System
					.getProperties().clone();
			for (Entry<String, String> entry : env.entrySet()) {
				svnStartCommand = svnStartCommand.replace("%".concat(
						entry.getKey()).concat("%"), entry.getValue());
			}
		}

		{

		}

		Runtime runtime = Runtime.getRuntime();
		runtime.addShutdownHook(new Thread() {
			@Override
			public void run() {
				stopSVN();
			}
		});
	}

	public void startSVN() {
		if (SVNProcess == null) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					System.out
							.println("SVNServer.SVNServer().new Runnable() {...}.run()");
					Runtime runtime = Runtime.getRuntime();
					try {
						System.out.println(svnStartCommand);

						SVNProcess = runtime.exec(svnStartCommand);
						BufferedReader bufferedReader;
						String out;
						if (SVNProcess != null) {

							bufferedReader = new BufferedReader(
									new InputStreamReader(SVNProcess
											.getErrorStream()));
							out = "";
							String messages = "";
							while ((out = bufferedReader.readLine()) != null) {
								System.err.println(out);
								messages += out;
							}
							if (messages.length() > 0) {
								JOptionPane.showMessageDialog(null, messages);
							}
						}
						if (SVNProcess != null) {
							System.out.println(":::::::::::::::");
							bufferedReader = new BufferedReader(
									new InputStreamReader(SVNProcess
											.getInputStream()));
							out = "";
							while ((out = bufferedReader.readLine()) != null)
								System.err.println(out);

						}
					} catch (IOException e) {
						throw new RuntimeException(e);
					}
				}
			}).start();
		} else {
			System.out.println("no SVNServer.startSVN()");
		}
	}

	public void stopSVN() {
		if (SVNProcess != null) {
			SVNProcess.destroy();
			SVNProcess = null;
		}
	}

	public boolean isAlive() {
		return SVNProcess != null;
	}

}
